package com.iweb.controller;

import com.iweb.model.User;
import com.iweb.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 南京艾瑞
 * 作者: jack
 * 时间: 2020-06-29 15:26
 * 描述: UserController
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    // 查询所有的用户
    @GetMapping
    public List<User> findAll() {
        List<User> users = userService.findAll();
        return users;
    }

    // 更具id查询
    @GetMapping("{id}")
    public User findById(@PathVariable String id) {
        User user = userService.findById(id);
        return user;
    }

    // 更具id修改
    @ApiOperation("根据id修改用户")
    @PutMapping("{id}")
    public User updateById(@PathVariable String id, @RequestBody User user) {
        user.setId(id);
        User dataUser = userService.updateById(user);
        return dataUser;
    }

    // 根据id删除
    @DeleteMapping("{id}")
    public Object deleteById(@PathVariable String id) {
        userService.deleteById(id);
        return null;
    }

    // 新增用户
    @PostMapping
    public User addUser(@RequestBody User user) {
        user = userService.addUser(user);
        return user;
    }

    // 分页
    @GetMapping("{current}/{size}")
    public List<User> findUsersByPage(@PathVariable Integer current, @PathVariable Integer size) {
        List<User> users = userService.findUsersByPage(current, size);
        return users;
    }

    // 条件查询
    @PostMapping("findByUser")
    public List<User> findByUser(@RequestBody User user) {
        List<User> users = userService.findByUser(user);
        return users;
    }

}
